package main;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import tools.XMLReader;
import tools.getProcess;

/**
* Filename:    MainControlDass.java
* @author:     yyi
* @version:    1.0
* @date:   2018-12-3
* @Description:
 */

public class MainControlDass {
	private static ServerSocket socket;
	public static void startDass(){
        try {
            String commandExe = XMLReader.getInstance().getPath();
            getProcess.executeCmd(commandExe);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	public static void main(String[] args) {
		System.out.println("-------7004-------");
        try {
        	socket = new ServerSocket(7004);
			byte getbytes[] = new byte[1024];
			while(true){
				Socket getCommandSocket = socket.accept();
				InputStream getCommandStream = getCommandSocket.getInputStream();
				//int msg = getCommandStream.available();
                int readCount = getCommandStream.read(getbytes);
				if (readCount > 0) {
					String command = (new String(getbytes)).trim();
					System.out.println(command);
					if(command.equals("STARTCONTROLDASS")){
						//查找进程中是否有DASS.exe,没有就去启动
						 boolean flag = getProcess.findProcess("ReliabilityDASS.exe");
						if(flag == true){
							String pid = getProcess.getPID();
                            System.out.println("pid:"+pid);
                            Runtime.getRuntime().exec("taskkill /F /PID "+pid);
							try {
								Thread.sleep(500);
								//关闭后启动
								startDass();
                                boolean flag1 = getProcess.findProcess("ReliabilityDASS.exe");
                                if (flag1 == true){
                                    System.out.println("------杀死进程后启动ReliabilityDASS成功------");
                                }
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}else{
							//启动DASS
							System.out.println("------启动ReliabilityDASS中------");
                            startDass();
                            boolean flag1 = getProcess.findProcess("ReliabilityDASS.exe");
                            if (flag1 == true){
                                System.out.println("------启动ReliabilityDASS成功------");
                            }

						}
					}
					
				}	
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
